// 数据加载器
class DataLoader {
    // 缓存已加载的数据
    static cache = {};

    // 加载JSON数据
    static async loadJSON(path) {
        // 如果缓存中已有数据，直接返回
        if (this.cache[path]) {
            return this.cache[path];
        }

        try {
            const response = await fetch(path);
            if (!response.ok) {
                throw new Error(`无法加载数据: ${response.status} ${response.statusText}`);
            }
            const data = await response.json();
            // 将数据存入缓存
            this.cache[path] = data;
            return data;
        } catch (error) {
            console.error(`加载数据错误 (${path}):`, error);
            return null;
        }
    }

    // 加载配置数据
    static async loadConfig() {
        return await this.loadJSON('data/config.json');
    }

    // 加载首页特性数据
    static async loadFeatures() {
        return await this.loadJSON('data/features.json');
    }

    // 加载首页时刻数据
    static async loadMoments() {
        return await this.loadJSON('data/moments.json');
    }

    // 加载相册数据
    static async loadAlbums() {
        return await this.loadJSON('data/albums.json');
    }

    // 加载日志数据
    static async loadDiary() {
        return await this.loadJSON('data/diary.json');
    }

    // 加载纪念日数据
    static async loadMemory() {
        return await this.loadJSON('data/memory.json');
    }

    // 格式化日期
    static formatDate(dateString) {
        const date = new Date(dateString);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        
        return `${year}年${month}月${day}日`;
    }

    // 计算两个日期之间的天数
    static daysBetween(date1, date2) {
        const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
        const diffTime = Math.abs(date2.getTime() - date1.getTime());
        return Math.round(diffTime / oneDay);
    }

    // 计算在一起的天数
    static async calculateDaysTogether() {
        const config = await this.loadConfig();
        if (!config) return 0;

        const startDate = new Date(config.loveStartDate);
        const today = new Date();
        
        return this.daysBetween(startDate, today);
    }

    // 获取相对时间描述
    static getRelativeTimeDescription(dateString) {
        const date = new Date(dateString);
        const now = new Date();
        const diffDays = this.daysBetween(now, date);
        
        if (diffDays === 0) {
            return '今天';
        } else if (diffDays === 1) {
            return date > now ? '明天' : '昨天';
        } else if (diffDays < 7) {
            return date > now ? `${diffDays}天后` : `${diffDays}天前`;
        } else if (diffDays < 30) {
            const weeks = Math.floor(diffDays / 7);
            return date > now ? `${weeks}周后` : `${weeks}周前`;
        } else if (diffDays < 365) {
            const months = Math.floor(diffDays / 30);
            return date > now ? `${months}个月后` : `${months}个月前`;
        } else {
            const years = Math.floor(diffDays / 365);
            return date > now ? `${years}年后` : `${years}年前`;
        }
    }
}

// 导出数据加载器
window.DataLoader = DataLoader; 